package com.mashibing.mall.coupon.service.impl;

import com.mashibing.common.dto.SkuReductionDTO;
import com.mashibing.common.exception.BusinessException;
import com.mashibing.mall.coupon.entity.MemberPriceEntity;
import com.mashibing.mall.coupon.entity.SkuLadderEntity;
import com.mashibing.mall.coupon.service.MemberPriceService;
import com.mashibing.mall.coupon.service.SkuLadderService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mashibing.common.utils.PageUtils;
import com.mashibing.common.utils.Query;

import com.mashibing.mall.coupon.dao.SkuFullReductionDao;
import com.mashibing.mall.coupon.entity.SkuFullReductionEntity;
import com.mashibing.mall.coupon.service.SkuFullReductionService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 商品满减信息 Service 实现类
 *
 * @author ROGzhongyang
 * @email inspiron149@163.com
 * @date 2024-11-18 19:53:35
 */
@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Resource
    private SkuLadderService skuLadderService;

    @Resource
    private MemberPriceService memberPriceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    /**
     * 商品满减信息 - 保存 sku 促销信息
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveSkuReduction(SkuReductionDTO skuReductionDTO) {

        System.out.println("skuReductionDTO = " + skuReductionDTO);

        // 5.3 保存满减信息，折扣，会员价
        // mall_sms: sms_sku_ladder sms_full_reduction sms_member_price
        // 1.折扣信息
        // 获取满减件数
        int fullCount = skuReductionDTO.getFullCount();

        // 如果满减件数小于等于0，则抛出异常
        if (fullCount <= 0) {
            throw new BusinessException("商品满减件数必须大于0");
        }

        // 构建待添加的商品阶梯价格
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();

        // 设置 sku_id、满减件数、折扣、是否叠加其他优惠
        skuLadderEntity.setSkuId(skuReductionDTO.getSkuId());
        skuLadderEntity.setFullCount(fullCount);
        skuLadderEntity.setDiscount(skuReductionDTO.getDiscount());
        skuLadderEntity.setAddOther(skuReductionDTO.getCountStatus());

        // 保存商品阶梯价格
        skuLadderService.save(skuLadderEntity);

        // 2.满减信息
        // 获取满减金额
        BigDecimal fullPrice = skuReductionDTO.getFullPrice();

        System.out.println("fullPrice = " + fullPrice);

        // 如果满减金额小于等于0，则抛出异常
        if (fullPrice.compareTo(new BigDecimal("0")) <= 0 ) {
            throw new BusinessException("商品满减金额必须大于0");
        }

        // 构建待添加的商品满减信息
        SkuFullReductionEntity fullReductionEntity = new SkuFullReductionEntity();

        // 设置 sku_id、满减金额、优惠金额、是否参与其他优惠
        fullReductionEntity.setSkuId(skuReductionDTO.getSkuId());
        fullReductionEntity.setFullPrice(skuReductionDTO.getFullPrice());
        fullReductionEntity.setReducePrice(skuReductionDTO.getReducePrice());
        fullReductionEntity.setAddOther(skuReductionDTO.getPriceStatus());

        // 保存商品满减信息
        this.save(fullReductionEntity);

        // 3.会员价信息
        List<MemberPriceEntity> memberPriceEntities = skuReductionDTO.getMemberPrice().stream().map(item -> {

            // 构建待添加的会员价信息
            MemberPriceEntity priceEntity = new MemberPriceEntity();

            // 设置 sku_id、会员等级、会员价、是否可叠加
            priceEntity.setSkuId(skuReductionDTO.getSkuId());
            priceEntity.setMemberLevelId(item.getId());
            priceEntity.setMemberPrice(item.getPrice());
            priceEntity.setAddOther(1); // 是否可叠加

            return priceEntity;
        }).collect(Collectors.toList());

        // 保存会员价信息
        memberPriceService.saveBatch(memberPriceEntities);
    }
}
